package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanyCustomGroupingAddDTO;
import org.jsola.hr.dto.CompanyCustomGroupingUpdateDTO;
import org.jsola.hr.entity.CompanyCustomGroupingDO;
import org.jsola.hr.query.CompanyCustomGroupingQuery;
import org.jsola.hr.service.ICompanyCustomGroupingService;
import org.jsola.hr.vo.CompanyCustomGroupingListVO;
import org.jsola.hr.vo.CompanyCustomGroupingVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;


/**
 * 自定义用工单位分组
 *
 * @author zhr
 */
@RestController("hrCompanyCustomGroupingController")
@RequestMapping(HrConstants.API_PATH + "/v1/companyCustomGroupings")
public class CompanyCustomGroupingController implements ICompanyCustomGroupingControllerAPI {

    @Autowired
    private ICompanyCustomGroupingService companyCustomGroupingService;

    @Override
    @PostMapping
    public Result<CompanyCustomGroupingVO> save(@Valid @RequestBody CompanyCustomGroupingAddDTO companyCustomGroupingAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增自定义用工单位分组权限校验

        return Result.success(
                companyCustomGroupingService.save(companyCustomGroupingAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyCustomGroupingUpdateDTO companyCustomGroupingUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改自定义用工单位分组权限校验

        int count = companyCustomGroupingService.update(companyCustomGroupingUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{companyCustomGroupingId}")
    public Result<Integer> delete(@PathVariable Long companyCustomGroupingId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除自定义用工单位分组权限校验

        int count = companyCustomGroupingService.deleteByIds(tokenUser, companyCustomGroupingId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] companyCustomGroupingIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除自定义用工单位分组权限校验

        int count = companyCustomGroupingService.deleteByIds(tokenUser, companyCustomGroupingIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companyCustomGroupingId}")
    public Result<CompanyCustomGroupingVO> findById(@PathVariable Long companyCustomGroupingId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看自定义用工单位分组权限校验

        return Result.success(
            companyCustomGroupingService.selectById(companyCustomGroupingId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyCustomGroupingListVO>> listAll(@Valid CompanyCustomGroupingQuery companyCustomGroupingQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看自定义用工单位分组权限校验

        return Result.success(
            companyCustomGroupingService.select(companyCustomGroupingQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyCustomGroupingListVO>> page(@Valid CompanyCustomGroupingQuery companyCustomGroupingQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            companyCustomGroupingService.selectPage(companyCustomGroupingQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectBySite")
    public Result<List<CompanyCustomGroupingListVO>> selectBySite(@Valid Integer yearly, Integer monthly, String moduleCode) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        List<CompanyCustomGroupingDO> companyCustomGroupingDOList = companyCustomGroupingService
                .selectAndSaveBySiteAndMonthly(yearly, monthly, moduleCode, tokenUser);
        return Result.success(
                companyCustomGroupingDOList.parallelStream().map(companyCustomGroupingDO
                        -> companyCustomGroupingDO.to(CompanyCustomGroupingListVO.class)).collect(Collectors.toList())
        );
    }


}

